package org.zpmis.api.http;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zpmis.api.business.SysRoleWebService;
import org.zpmis.common.base.BaseController;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.domain.role.qo.*;

import javax.annotation.Resource;

/**
* created with CodeGenerator
* description:
* @author  Yang
* date: 2021-5-31
* time: 14:00:27
*/
@RestController
@RequestMapping("/role")
@Slf4j
public class SysRoleController extends BaseController{

    @Resource
    private SysRoleWebService sysRoleWebService;

    /**
     * 获取所有用户角色 用户新增修改下拉使用
     * @return
     */
    @PostMapping("/allSysRole")
    public PlatformResult allSysRole(){
        try {
            return PlatformResult.success(sysRoleWebService.allSysRole());
        } catch (BusinessException e) {
            log.error("[SysRoleController]---allSysRole--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---allSysRole--->Exception:",e);
            return PlatformResult.failure();
        }
    }
    /**
     * 新增角色信息
     * @param addSysRoleQo
     * @return
     */
    @PostMapping("/addSysRole")
    public PlatformResult addSysRole(@Validated @RequestBody AddSysRoleQo addSysRoleQo){
        try {
            sysRoleWebService.addSysRole(addSysRoleQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysRoleController]---addSysRole--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---addSysRole--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 修改角色信息
     * @param editSysRoleQo
     * @return
     */
    @PostMapping("/editSysRole")
    public PlatformResult editSysRole(@Validated @RequestBody EditSysRoleQo editSysRoleQo){
        try {
            sysRoleWebService.editSysRole(editSysRoleQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysRoleController]---editSysRole--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---editSysRole--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 删除角色信息
     * @param delSysRoleQo
     * @return
     */
    @PostMapping("/delSysRole")
    public PlatformResult delSysRole(@Validated @RequestBody DelSysRoleQo delSysRoleQo){
        try {
            sysRoleWebService.delSysRole(delSysRoleQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysRoleController]---delSysRole--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---delSysRole--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 角色列表查询
     * @param sysRoleListQo
     * @return
     */
    @PostMapping("/sysRoleList")
    public PlatformResult sysRoleList(@RequestBody SysRoleListQo sysRoleListQo){
        try {

            return PlatformResult.success(sysRoleWebService.sysRoleList(sysRoleListQo));
        } catch (BusinessException e) {
            log.error("[SysRoleController]---sysRoleList--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---sysRoleList--->Exception:",e);
            return PlatformResult.failure();
        }
    }
    /**
     * 角色分配用户
     * @param roleDistributionUserQo
     * @return
     */
    @PostMapping("/roleDistributionUser")
    public PlatformResult roleDistributionUser(@Validated @RequestBody RoleDistributionUserQo roleDistributionUserQo){
        try {
            sysRoleWebService.roleDistributionUser(roleDistributionUserQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysRoleController]---roleDistributionUser--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---roleDistributionUser--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 角色分配权限
     * @param roleDistributionPermissionQo
     * @return
     */
    @PostMapping("/roleDistributionPermission")
    public PlatformResult roleDistributionPermission(@Validated @RequestBody RoleDistributionPermissionQo roleDistributionPermissionQo){
        try {
            sysRoleWebService.roleDistributionPermission(roleDistributionPermissionQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysRoleController]---roleDistributionPermission--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---roleDistributionPermission--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 根据角色id 查询该角色下的用户集合
     * @param findUserListByRoleIdQo
     * @return
     */
    @PostMapping("/findSysUserListByRoleId")
    public PlatformResult findSysUserListByRoleId(@Validated @RequestBody FindListByRoleIdQo findUserListByRoleIdQo){
        try {
            return PlatformResult.success(sysRoleWebService.findSysUserListByRoleId(findUserListByRoleIdQo));
        } catch (BusinessException e) {
            log.error("[SysRoleController]---findUserListByRoleId--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---findUserListByRoleId--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 根据角色id查询该角色下权限集合
     * @param findUserListByRoleIdQo
     * @return
     */
    @PostMapping("/findPermissionListByRoleId")
    public PlatformResult findPermissionListByRoleId(@Validated @RequestBody FindListByRoleIdQo findUserListByRoleIdQo){
        try {
            return PlatformResult.success(sysRoleWebService.findPermissionListByRoleId(findUserListByRoleIdQo));
        } catch (BusinessException e) {
            log.error("[SysRoleController]---findPermissionListByRoleId--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---findPermissionListByRoleId--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 角色分配菜单
     * @param roleDistributionMenuQo
     * @return
     */
    @PostMapping("/roleDistributionMenu")
    public PlatformResult roleDistributionMenu(@Validated @RequestBody RoleDistributionMenuQo roleDistributionMenuQo){
        try {
            sysRoleWebService.roleDistributionMenu(roleDistributionMenuQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysRoleController]---roleDistributionMenu--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---roleDistributionMenu--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 根据角色id 查询该角色下的菜单集合
     * @param findUserListByRoleIdQo
     * @return
     */
    @PostMapping("/findSysMenuListByRoleId")
    public PlatformResult findSysMenuListByRoleId(@Validated @RequestBody FindListByRoleIdQo findUserListByRoleIdQo){
        try {
            return PlatformResult.success(sysRoleWebService.findSysMenuListByRoleId(findUserListByRoleIdQo));
        } catch (BusinessException e) {
            log.error("[SysRoleController]---findSysMenuListByRoleId--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysRoleController]---findSysMenuListByRoleId--->Exception:",e);
            return PlatformResult.failure();
        }
    }
}
